package org.boulderfoodrescue.webapp.server.timeSlots;

import java.util.List;
import java.util.logging.Logger;

import org.boulderfoodrescue.webapp.client.rpc.TimeSlotService;
import org.boulderfoodrescue.webapp.server.pickups.PickupDatabase;
import org.boulderfoodrescue.webapp.server.users.UserAccountsDatabase;
import org.boulderfoodrescue.webapp.shared.OneTimeEvent;
import org.boulderfoodrescue.webapp.shared.Pickup;
import org.boulderfoodrescue.webapp.shared.RecurringEvent;
import org.boulderfoodrescue.webapp.shared.UserAccount;
import org.boulderfoodrescue.webapp.shared.UserAccountDetails.AccessLevel;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

@SuppressWarnings("serial")
public class TimeSlotServiceImpl extends RemoteServiceServlet implements TimeSlotService {
	private static final Logger log = Logger.getLogger(TimeSlotServiceImpl.class.getName());

	@Override
	public Long saveOneTimeEvent(OneTimeEvent event) {
		UserService userService = UserServiceFactory.getUserService();
		User person = userService.getCurrentUser();
		if(person == null){
			log.warning("User is not logged in. Cannot execute saveLocationData() call");
			return null;
		}
		UserAccount user = UserAccountsDatabase.getUser(person.getEmail());
		if(user.getAccessLevel().compareTo(AccessLevel.Volunteer) < 0)
		{
			log.warning("User does not have the proper privleges to perform this operation: "+person.getEmail() + " access level = " + user.getAccessLevel());
			return null;
		}
		if(event.getDatabaseKey() != null && event.getDatabaseKey() != 0){
			return OneTimeEventDatabase.updateOneTimeEvent(event); 
		}
		return OneTimeEventDatabase.addOneTimeEvent(event);
	}

	@Override
	public List<OneTimeEvent> getOneTimeEvents(Long locationKey) {
		UserService userService = UserServiceFactory.getUserService();
		User person = userService.getCurrentUser();
		if(person == null){
			log.warning("User is not logged in. Cannot execute saveLocationData() call");
			return null;
		}
		UserAccount user = UserAccountsDatabase.getUser(person.getEmail());
		if(user.getAccessLevel().compareTo(AccessLevel.Volunteer) < 0)
		{
			log.warning("User does not have the proper privleges to perform this operation: "+person.getEmail() + " access level = " + user.getAccessLevel());
			return null;
		}
		return OneTimeEventDatabase.getOneTimeEvents(locationKey, 1000);
	}

	@Override
	public RecurringEvent saveRecurringEvent(RecurringEvent event) {
		UserService userService = UserServiceFactory.getUserService();
		User person = userService.getCurrentUser();
		if(person == null){
			log.warning("User is not logged in. Cannot execute saveLocationData() call");
			return null;
		}
		UserAccount user = UserAccountsDatabase.getUser(person.getEmail());
		if(user.getAccessLevel().compareTo(AccessLevel.Volunteer) < 0)
		{
			log.warning("User does not have the proper privleges to perform this operation: "+person.getEmail() + " access level = " + user.getAccessLevel());
			return null;
		}
		if(event.getDatabaseKey() != null && event.getDatabaseKey() != 0){
			return RecurringEventDatabase.updateRecurringEvent(event); 
		}
		return RecurringEventDatabase.addRecurringEvent(event);
	}

	@Override
	public List<RecurringEvent> getRecurringEvent(Long locationKey) {
		UserService userService = UserServiceFactory.getUserService();
		User person = userService.getCurrentUser();
		if(person == null){
			log.warning("User is not logged in. Cannot execute saveLocationData() call");
			return null;
		}
		UserAccount user = UserAccountsDatabase.getUser(person.getEmail());
		if(user.getAccessLevel().compareTo(AccessLevel.Volunteer) < 0)
		{
			log.warning("User does not have the proper privleges to perform this operation: "+person.getEmail() + " access level = " + user.getAccessLevel());
			return null;
		}
		return RecurringEventDatabase.getRecurringEvents(locationKey, 1000);
	}

	@Override
	public List<Pickup> deleteOneTimeEvent(OneTimeEvent event) {
		UserService userService = UserServiceFactory.getUserService();
		User person = userService.getCurrentUser();
		if(person == null){
			log.warning("User is not logged in. Cannot execute deleteOneTimeEvent() call");
			return null;
		}
		UserAccount user = UserAccountsDatabase.getUser(person.getEmail());
		if(user.getAccessLevel().compareTo(AccessLevel.Volunteer) < 0)
		{
			log.warning("User does not have the proper privleges to perform this operation: "+person.getEmail() + " access level = " + user.getAccessLevel());
			return null;
		}
		
		List<Pickup> list = PickupDatabase.getPickupsOneTimeEvent(event.getDatabaseKey());
		if(list.size() == 0)
			OneTimeEventDatabase.removeOneTimeToEntity(event);
		return list;
	}

	@Override
	public List<Pickup> deleteRecurringEvent(RecurringEvent event) {
		UserService userService = UserServiceFactory.getUserService();
		User person = userService.getCurrentUser();
		if(person == null){
			log.warning("User is not logged in. Cannot execute deleteRecurringEvent() call");
			return null;
		}
		UserAccount user = UserAccountsDatabase.getUser(person.getEmail());
		if(user.getAccessLevel().compareTo(AccessLevel.Volunteer) < 0)
		{
			log.warning("User does not have the proper privleges to perform this operation: "+person.getEmail() + " access level = " + user.getAccessLevel());
			return null;
		}
		
		List<Pickup> list = PickupDatabase.getPickupsOneTimeEvent(event.getDatabaseKey());
		if(list.size() == 0)
			RecurringEventDatabase.removeRecurringEvent(event);
		return list;
	}

}
